import fs from 'fs';

export function file_exists(file: string) {
	return new Promise<boolean>((resolve) => {
		fs.exists(file, (exists) => {
			resolve(exists);
		});
	});
}

export function read_file(file: string) {
	return new Promise<string>((resolve, reject) => {
		fs.readFile(file, 'utf8', (err, data) => {
			if (err) {
				reject(err);
			} else {
				resolve(data);
			}
		});
	});
}

export function write_file(file: string, data: string | Buffer) {
	return new Promise<void>((resolve, reject) => {
		fs.writeFile(file, data, (err) => {
			if (err) {
				reject(err);
			} else {
				resolve();
			}
		});
	});
}

export function unlink(file: string) {
	return new Promise<void>((resolve, reject) => {
		fs.unlink(file, (err) => {
			if (err) {
				reject(err);
			} else {
				resolve();
			}
		});
	});
}
